export default {
  namespaced: true,
  state: {
    historyList: []
  },
  getters: {
    historyList: state => {
      if (state.historyList.length === 0) {
        try {
          const historyList = localStorage.getItem('historyList') || []
          state.historyList = JSON.parse(historyList)
        } catch (e) {
          console.error(e)
        }
      }
      return state.historyList
    }
  },
  mutations: {
    setHistoryList(state, value) {
      let date = new Date().getTime()
      let obj = {
        value: value,
        dateTime: date
      }
      let oldData = JSON.parse(JSON.stringify(state.historyList))
      oldData.forEach((item, index) => {
        if (Math.floor(date - item.dateTime) / (24 * 60 * 60 * 1000) > 30) {
          oldData.splice(index, 1)
        }
      })
      oldData.push(obj)
      state.historyList = oldData
      // state.historyList.push(obj)
      localStorage.setItem('historyList', JSON.stringify(state.historyList))
    },
    delHistoryList(state) {
      state.historyList = []
      localStorage.setItem('historyList', JSON.stringify(state.historyList))
    }
  }
}
